package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.MdmDriveLoginLogDTO;
import com.kaili.basic.pojo.dto.MdmDriveLoginLogQueryDto;
import com.kaili.basic.pojo.dto.MdmSysLogQueryDto;
import com.kaili.basic.pojo.dto.MdmSysLoginLogQueryDto;
import com.kaili.basic.pojo.entity.MdmDriveLoginLog;
import com.kaili.basic.pojo.entity.MdmSysLoginLog;
import com.kaili.basic.pojo.vo.MdmSysLogExportVO;
import com.kaili.basic.service.IMdmLogService;
import com.kaili.common.pojo.entity.MdmSysLogEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * Created by renzhikun on 18-6-14.
 */
@Service
public class IMdmLogServiceImpl implements IMdmLogService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Page<MdmSysLoginLog> querySysLoginLog(MdmSysLoginLogQueryDto dto, String collectionName) {
        Page<MdmSysLoginLog> page=new Page<>(dto.getPage(),dto.getPageSize());
        //1.设置分页
        Pageable pageable=new PageRequest(dto.getPage()==1?0:dto.getPage(),dto.getPageSize());
        //2.时间倒序
        Sort sort=new Sort(Sort.Direction.DESC,"loginTime");
        //3.过滤参数
        Criteria criteria =new Criteria();
        setSysLoginLogWhere(criteria,dto);

        //4.组合查询条件
        Query query=new Query(criteria);
        query.with(sort);
        long count = mongoTemplate.count(query, MdmSysLoginLog.class, collectionName);
        query.with(pageable);
        page.setRecords(mongoTemplate.find(query,MdmSysLoginLog.class, collectionName));
        page.setTotal(count);
        return page;
    }


    @Override
    public Page<MdmSysLogEntity> querySysLog(String collectionName, MdmSysLogQueryDto dto) {
        Page<MdmSysLogEntity> page=new Page<>(dto.getPage(),dto.getPageSize());
        //1.设置分页
        Pageable pageable=new PageRequest(dto.getPage()==1?0:dto.getPage(),dto.getPageSize());
        //2.时间倒序
        Sort sort=new Sort(Sort.Direction.DESC,"operatorTime");
        //3.过滤参数
        Criteria criteria =new Criteria();
        setSysLogWhere(criteria,dto);
        //4.组合查询条件
        Query query=new Query(criteria);
        query.with(sort);
        long count = mongoTemplate.count(query, MdmSysLogEntity.class, collectionName);
        query.with(pageable);
        page.setRecords(mongoTemplate.find(query,MdmSysLogEntity.class, collectionName));
        page.setTotal(count);
        return page;
    }

    @Override
    public List<MdmSysLogExportVO> querySysLogExport(String collectionName, MdmSysLogQueryDto dto) {
        //1.时间倒序
        Sort sort=new Sort(Sort.Direction.DESC,"operatorTime");
        //2.过滤参数
        Criteria criteria =new Criteria();
        setSysLogWhere(criteria,dto);
        //3.组合查询条件
        Query query=new Query(criteria);
        query.with(sort);
        return mongoTemplate.find(query,MdmSysLogExportVO.class, collectionName);
    }

    @Override
    public MdmSysLoginLog queryLogOne(String username, String collectionName) {
        Query query=new Query(Criteria.where("userName").is(username));
        query.with(new Sort(Sort.Direction.DESC,"loginTime")).with(new PageRequest(0,1));
        List<MdmSysLoginLog> list = mongoTemplate.find(query, MdmSysLoginLog.class, collectionName);
        return list.size()>0?list.get(0):null;
    }

    @Override
    public void saveSysLoginLog(MdmSysLoginLog logBean, String collectionName) {
        mongoTemplate.save(logBean,collectionName);
    }

    @Override
    public List<MdmSysLoginLog> querySysLogExport(MdmSysLoginLogQueryDto dto, String collectionName) {
        //1.时间倒序
        Sort sort=new Sort(Sort.Direction.DESC,"loginTime");
        //2.过滤参数
        Criteria criteria =new Criteria();
        setSysLoginLogWhere(criteria,dto);
        //3.组合查询条件
        Query query=new Query(criteria);
        query.with(sort);
        return mongoTemplate.find(query,MdmSysLoginLog.class, collectionName);
    }

    @Override
    public void saveDriveLogs(MdmDriveLoginLogDTO dto, String collectionName) {
        MdmDriveLoginLog entity=new MdmDriveLoginLog();
        BeanUtils.copyProperties(dto,entity);
        mongoTemplate.save(entity,collectionName);
    }

    @Override
    public Page<MdmDriveLoginLog> queryDriveLogs(String collectionName, MdmDriveLoginLogQueryDto queryDto) {
        Page<MdmDriveLoginLog> page=new Page<>(queryDto.getPage(),queryDto.getPageSize());
        //1.设置分页
        Pageable pageable=new PageRequest(queryDto.getPage()==1?0:queryDto.getPage(),queryDto.getPageSize());
        //1.时间倒序
        Sort sort=null;
        if(0==queryDto.getTimeType()){
            sort=new Sort(Sort.Direction.DESC,"loginTime");
        }else{
            sort=new Sort(Sort.Direction.DESC,"registerTime");
        }
        //3.过滤参数
        Criteria criteria =new Criteria();
        setDriveLoginLogWhere(criteria,queryDto);
        //4.组合查询条件
        Query query=new Query(criteria);
        query.with(sort);
        long count = mongoTemplate.count(query, MdmDriveLoginLog.class, collectionName);
        query.with(pageable);
        page.setRecords(mongoTemplate.find(query,MdmDriveLoginLog.class, collectionName));
        page.setTotal(count);
        return page;
    }

    @Override
    public List<MdmDriveLoginLog> queryDriveLogExport(MdmDriveLoginLogQueryDto dto, String collectionName) {
        //1.时间倒序
        Sort sort=null;
        if(0==dto.getTimeType()){
            sort=new Sort(Sort.Direction.DESC,"loginTime");
        }else{
            sort=new Sort(Sort.Direction.DESC,"registerTime");
        }
        //2.过滤参数
        Criteria criteria =new Criteria();
        setDriveLoginLogWhere(criteria,dto);
        //3.组合查询条件
        Query query=new Query(criteria);
        query.with(sort);
        return mongoTemplate.find(query,MdmDriveLoginLog.class, collectionName);
    }


    public void setSysLogWhere(Criteria criteria,MdmSysLogQueryDto dto){
        if(StringUtils.isNoneBlank(dto.getOrgan())){
            criteria.and("organ").regex(dto.getOrgan());
        }
        if(StringUtils.isNoneBlank(dto.getOperatorName())){
            criteria.and("operatorName").regex(dto.getOperatorName());
        }
        if(StringUtils.isNoneBlank(dto.getOperatorModule())){
            criteria.and("operatorModule").regex(dto.getOperatorModule());
        }
        if(StringUtils.isNoneBlank(dto.getOperatorType())){
            criteria.and("operatorType").is(dto.getOperatorType());
        }
        if(dto.getStartTime()!=null&&dto.getEndTime()!=null){
            criteria.and("operatorTime").gte(dto.getStartTime()).lte(dto.getEndTime());
        }
    }

    public void setSysLoginLogWhere(Criteria criteria,MdmSysLoginLogQueryDto dto){
        if(StringUtils.isNoneBlank(dto.getOrgan())){
            criteria.and("organ").regex(dto.getOrgan());
        }
        if(StringUtils.isNoneBlank(dto.getName())){
            criteria.and("name").regex(dto.getName());
        }
        if(dto.getStartTime()!=null&&dto.getEndTime()!=null){
            criteria.and("loginTime").gte(dto.getStartTime()).lte(dto.getEndTime());
        }
    }

    public void setDriveLoginLogWhere(Criteria criteria,MdmDriveLoginLogQueryDto dto){
        if(StringUtils.isNoneBlank(dto.getOrgan())){
            criteria.and("organ").regex(dto.getOrgan());
        }
        if(StringUtils.isNoneBlank(dto.getDriveName())){
            criteria.and("name").regex(dto.getDriveName());
        }
        if(dto.getStartTime()!=null&&dto.getEndTime()!=null){
            if(0==dto.getTimeType()){
                criteria.and("loginTime").gte(dto.getStartTime()).gte(dto.getEndTime());
            }else{
                criteria.and("registerTime").gte(dto.getStartTime()).gte(dto.getEndTime());
            }
        }
    }

}
